package com.lovo.service.impl;

import com.lovo.dao.StudentDAO;
import com.lovo.model.dos.StudentDO;
import com.lovo.model.dto.StudentDTO;
import com.lovo.model.vo.PageVO;
import com.lovo.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDAO studentDAO;
    @Override
    public PageVO findAll(Integer page, Integer size) {
        Page<StudentDO> pageObj = studentDAO.findAll(PageRequest.of(page-1,size));
        List<StudentDTO> studentDTOList = new ArrayList();
        for (StudentDO studentDO:pageObj.getContent()) {
            StudentDTO studentDTO = new StudentDTO();
            BeanUtils.copyProperties(studentDO,studentDTO,"classDO");
            studentDTO.setClassName(studentDO.getClassDO().getClassName());
            studentDTOList.add(studentDTO);
        }
        return new PageVO(pageObj.getNumber()+1,pageObj.getSize(),pageObj.getTotalElements(),studentDTOList);
    }

    @Override
    public void delete(Integer id) {
        studentDAO.deleteById(id);
    }
}
